const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const BundleAnalyzerPlugin =
  require("webpack-bundle-analyzer").BundleAnalyzerPlugin;

module.exports = {
  // ...
  plugins: [new BundleAnalyzerPlugin()],
};

module.exports = {
  entry: "./src/index.js",
  output: {
    path: path.resolve(__dirname, "dist"),
    // filename: "bundle.js",
    filename: "[name].[chunkhash].js",
    // publicPath: "/",
    // clean: true,
  },
  optimization: {
    splitChunks: {
      cacheGroups: {
        echarts: {
          test: /[\\/]node_modules[\\/](echarts|zrender)[\\/]/,
          name: "echarts",
          priority: 10,
          chunks: "all",
        },
        commonLib: {
          test: /[\\/]node_modules[\\/]/,
          name: "commonLib",
          chunks: "all",
        },
      },
    },
  },
  devServer: {
    // static: path.join(__dirname, "dist"),
    static: {
      directory: path.join(__dirname, "src"), // where Webpack should serve static files from
    },
    compress: true,
    port: 9000,
    open: true,
    historyApiFallback: {
      index: `index.html`,
    },
    historyApiFallback: true, // 此行是新增的
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: ["babel-loader"],
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, "src", "index.html"),
    }),
    process.env.ANALYZE && new BundleAnalyzerPlugin(),
  ].filter(Boolean),
};
